<?php
/**
 * Created by PhpStorm.
 * User: shiyin
 * Date: 2020/6/16
 * Time: 10:32
 */

namespace Danmu\Traits;

use Danmu\qiniu;

trait SendTrait
{

    protected function sendAll($pubFd, $data, $type = 'text')
    {
        // $this->server->connections
        // 遍历所有websocket连接用户的fd，给所有用户推送

        $ws = $this->ws;
        $away = [];
        $content = null;
        foreach ($ws->connections as $fd) {
            // 需要先判断是否是正确的websocket连接，否则有可能会push失败
            if ($ws->isEstablished($fd)) {

                // 当前链接发送的消息
                if ($fd == $pubFd) {
                    $away['is_self'] = 1;
                    $prefix = '自己：';
                } else {
                    $away['is_self'] = 0;
                    $prefix = "用户{$pubFd}：";
                }

                switch ($type) {
                    case 'text':
                        $content = $prefix . $data;
                        break;
                    case 'record':
                        $away['path'] = $this->getPath($data);
                        $content = $prefix . '▁▂▃▄▅(点击播放)';
                        break;
                    case 'image':
                        $away['path'] = $this->getPath($data);
                        $content = $prefix . '图片消息';
                        break;
                }
                if (!is_null($content)) {
                    $response = $this->getResponse($content, $type, $away);
                    $ws->push($fd, $response);
                }
            }
        }
    }

    private function getPath($data)
    {
        return qiniu::$host . $data;
    }

    // 是当前链接发送的消息
    private function isLinkSend($fd, $pubFd)
    {
        return $fd == $pubFd;
    }

    protected function sendOne($fd, $data, $type = 'text')
    {
        $ws = $this->ws;

        // 需要先判断是否是正确的websocket连接，否则有可能会push失败
        if ($ws->isEstablished($fd)) {
            $ws->push($fd, $this->getResponse($data, $type));
        }
    }

    protected function ping($request)
    {
        return function () use ($request) {
            if ($this->ws->isEstablished($request->fd)) {

                $this->ws->push($request->fd, json_encode(['type' => 'ping']));
                $this->clients[$request->fd]['ping'] += 1;
                if ($this->clients[$request->fd]['ping'] >= 3) {
                    \Swoole\Timer::clear($this->clients[$request->fd]['timer_id']);
                    $res = $this->ws->disconnect($request->fd, 1000, "主动断开");
                    echo '主动断开连接:' . json_encode($res);
                }
            } else {

                \Swoole\Timer::clear($this->clients[$request->fd]['timer_id']);
                unset($this->clients[$request->fd]);
            }
        };
    }

    private function getResponse($data, $type = 'text', $away = [])
    {
        $response = [
            'type' => $type,
            'data' => $data
        ];
        foreach ($away as $key => $field) {
            $response[$key] = $field;
        }
        return json_encode($response);
    }
}